package com.mystyle.le.weekproject.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.beans.TalkList;
import com.mystyle.le.weekproject.sqlite.TilkSqlite;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.mystyle.le.weekproject.R.drawable.selector_talk_commend;
import static com.mystyle.le.weekproject.R.mipmap.ic_shoucang_pressed;

/**
 * Created by le on 2016/10/11.
 */
public class TalkFragmentAdapter extends BaseAdapter {
    private List<TalkList> lists;
    private Context context;
    private boolean isChock = false;
    private static TilkSqlite tilkSqlite;
    private SQLiteDatabase database;
    private String sql = null;
    private Cursor cursor;

    public void operatDb(String sql){
        SQLiteDatabase database=tilkSqlite.getReadableDatabase();
        database.execSQL(sql);
        database.close();
    }
    public static Cursor query(String sql){
        SQLiteDatabase database=tilkSqlite.getReadableDatabase();
        return database.rawQuery(sql, null);
    }

    public TalkFragmentAdapter(List<TalkList> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @Override
    public int getCount() {
        return lists==null?0:lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHold hold;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.talk_layout,parent,false);
            hold = new ViewHold();
            hold.talk_name = (TextView) convertView.findViewById(R.id.talk_name);
            hold.talk_createtime = (TextView) convertView.findViewById(R.id.talk_createtime);
            hold.talk_text = (TextView) convertView.findViewById(R.id.talk_text);

            hold.talk_imgurl = (ImageView) convertView.findViewById(R.id.talk_imgurl);

            hold.talk_commend = (Button) convertView.findViewById(R.id.talk_commend);
            hold.talk_collect = (Button) convertView.findViewById(R.id.talk_collect);
            hold.talk_share = (Button) convertView.findViewById(R.id.talk_share);
            convertView.setTag(hold);
        }else {
            hold = (ViewHold) convertView.getTag();
        }
        final TalkList talkList = lists.get(position);
        if (talkList!=null){
            hold.talk_name.setText(talkList.getName());
            hold.talk_createtime.setText(talkList.getCreatetime());
            hold.talk_text.setText(talkList.getText());
            hold.talk_commend.setText(talkList.getCommend());//点赞

        }
        tilkSqlite = new TilkSqlite(context);
        database = tilkSqlite.getReadableDatabase();

        hold.talk_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChock){
                    Toast.makeText(context,"取消收藏",Toast.LENGTH_SHORT).show();
                    hold.talk_collect.setBackgroundResource(R.drawable.ic_shoucang);
                    String name = talkList.getName();
                    sql="select * from data where name='"+name+"'";
                    cursor = query(sql);
                    if (cursor.moveToNext()){
                        sql = "delete from data where name = '"+name+"'";
                        operatDb(sql);
                    }
                    isChock = false;
                }else {
                    String name = talkList.getName();
                    String createtime = talkList.getCreatetime();
                    String text = talkList.getText();
                    String commend = talkList.getCommend();
                    sql="select * from data where name='"+name+"'";
                    cursor =query(sql);
                    if (cursor.moveToNext()){
                        isChock = true;
                        return;
                    }else {
                        Toast.makeText(context,"添加收藏",Toast.LENGTH_SHORT).show();
                        hold.talk_collect.setBackgroundResource(R.drawable.ic_shoucang_pressed);
                        sql = "insert into data(name,createtime,text,commend) values ('"+name+"','"+createtime+"','"+text+"','"+commend+"')";
                        operatDb(sql);
                    }

                }
            }
        });
        return convertView;
    }
    class ViewHold{
        TextView talk_name,talk_createtime,talk_text;
        ImageView talk_imgurl;
        Button talk_commend,talk_collect,talk_share;//点赞，分享，收藏
    }
}
